<template>
    <basic-container>
        <avue-crud :data="data" :option="option" v-model="obj" :page="page"
                   :before-open="beforeOpen"
                   @row-del="rowDel"
                   @row-update="rowUpdate"
                   @row-save="rowSave"
                   @refresh-change="refreshChange"
                   @search-change="searchChange"
                   @search-reset="searchReset"
                   @current-change="currentChange"
                   :before-close="beforeClose"
                   @size-change="sizeChange" @on-load="onLoad"></avue-crud>
    </basic-container>
</template>
<script>
    import {getList, getSellBuyer, addItem, editItem, deleteItem} from '@/api/account/userAccount/goodsSellScale'

    var dicScale = [
        {label: '1:9', value: '1:9'},
        {label: '2:8', value: '2:8'},
        {label: '3:7', value: '3:7'},
        {label: '4:6', value: '4:6'},
        {label: '5:5', value: '5:5'},
        {label: '6:4', value: '6:4'},
        {label: '7:3', value: '7:3'},
        {label: '8:2', value: '8:2'},
        {label: '9:1', value: '9:1'},
    ]
    var dicScaleSame = [
        {label: '5:5', value: '5:5'},
    ]
    export default {
        data() {
            return {
                dicScale:dicScale,
                dicBuyer: [],
                dicSell: [],
                page: {
                    pageSize: 10,
                    currentPage: 1,
                    total: 0
                },
                obj: {},
                data: [],
                type:'',
            }
        },
        computed:{
            option() {
                return {
                    border:true,
                    title: '',
                    page: false,
                    align: 'center',
                    menuAlign: 'center',
                    labelWidth: 140,
                    column: [
                        {
                            label: '货方',
                            prop: 'sellsSupplierId',
                            disabled: false,
                            type: 'select',
                            search:true,
                            row: true,
                            dicData: this.dicSell,
                            change: ({value}) => {
                                if(this.type=='edit') {
                                    this.option.column[0].disabled=true
                                    this.option.column[1].disabled=true
                                    if(value==this.obj.buysSupplierId) {
                                        this.option.column[2].disabled=true
                                        this.option.column[2].dicData = dicScaleSame
                                    }else {
                                        this.option.column[2].disabled=false
                                        this.option.column[2].dicData = dicScale
                                    } // 编辑时货销方不可编辑,如果货销方一致,比例也不可编辑
                                    return
                                }
                                if (value) {
                                    if(value ==this.obj.buysSupplierId ){
                                        this.obj.proportion=''
                                        this.obj.$proportion = ''
                                        this.option.column[2].dicData = dicScaleSame
                                    }else {
                                        this.option.column[2].dicData = dicScale
                                    }
                                }else {
                                    this.option.column[2].dicData = dicScale
                                }
                            },
                        },
                        {
                            label: '销方',
                            prop: 'buysSupplierId',
                            disabled:false,
                            type: 'select',
                            search:true,
                            row: true,
                            dicData: this.dicBuyer,
                            change: ({value}) => {
                                if(this.type=='edit') {
                                    this.option.column[0].disabled=true
                                    this.option.column[1].disabled=true
                                    if(this.obj.sellsSupplierId==value) {
                                        this.option.column[2].disabled=true
                                        this.option.column[2].dicData = dicScaleSame
                                    }else {
                                        this.option.column[2].disabled=false
                                        this.option.column[2].dicData = dicScale
                                    }
                                    return
                                }
                                if (value) {
                                    if(value ==this.obj.sellsSupplierId ){
                                        this.obj.proportion=''
                                        this.obj.$proportion = ''
                                        this.option.column[2].dicData = dicScaleSame
                                    }else {
                                        this.option.column[2].dicData = dicScale
                                    }
                                }else {
                                    this.option.column[2].dicData = dicScale
                                }
                            },
                        }, {
                            label: "货方销方佣金比例",
                            prop: "proportion",
                            row: true,
                            type: 'select',
                            dicData: dicScale,
                        }
                    ]
                }
            }
        },
        created() {
            this.getSellBuyerFn()
        },
        methods: {
            beforeOpen(done,type) {
                console.log('type',type)
                this.type=type
                if(type=='edit') {
                    this.option.column[0].disabled=true
                    this.option.column[1].disabled=true
                }
                done()
            },
            beforeClose(done,type) {
                    this.option.column[0].disabled=false
                    this.option.column[1].disabled=false
                this.getSellBuyerFn()
                done()
            },
            // 点击页数
            currentChange(currentPage) {
                this.page.currentPage = currentPage;
                this.onLoad(this.page)
            },
            // 点击页数
            sizeChange(pageSize) {
                this.page.pageSize = pageSize;
                this.onLoad(this.page)
            },
            onLoad(page, params = {}, done) {//页面初始化请求管理员列表
                getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
                    if (done) {
                        done();
                    }
                    //为了在删除最后一页的最后一条数据时能成功跳转回最后一页的上一页
                    if(res.data.data.length === 0 && this.page.currentPage > 1) {
                        this.page.currentPage -= 1;
                        this.onLoad(this.page);
                    }
                    this.data = res.data.data
                    this.page.total = res.data.count
                })
            },
            searchReset() {
                this.query = {};
                this.onLoad(this.page);
            },
            // 搜索框
            searchChange(params, done) {
                this.page.currentPage = 1
                this.query = params;
                this.onLoad(this.page, params, done);
            },
            refreshChange({page, searchForm}) {
                this.onLoad(this.page);
            },
            // 新增
            rowSave(row,  done,loading) {
                for(let k in this.obj) {
                    if(this.obj[k]=='') {
                        this.$message({
                            type: "error",
                            message: "请完善数据"
                        });
                        loading()
                        return
                    }
                }
                this.addItemFn(this.obj, done)
                this.getSellBuyerFn()
                this.onLoad(this.page)
            },
            // 编辑
            rowUpdate(row, index, done, loading) {
                let obj={
                    sellsSupplierId:this.obj.sellsSupplierId,
                    buysSupplierId:this.obj.buysSupplierId,
                    proportion:this.obj.proportion
                }
                console.log('1111',this.obj)
                for(let k in obj) {
                    if(this.obj[k]=='') {
                        this.$message({
                            type: "error",
                            message: "请完善数据"
                        });
                        loading()
                        return
                    }
                }
                this.obj.id = row.id
                this.editItemFn(this.obj, done)
                this.getSellBuyerFn()
            },
            // 删除
            rowDel(row, index) {
                let obj = {
                    id: row.id
                }
				this.$confirm('确定删除该比例?', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
                    deleteItem(obj).then(res => {
                        if (res.data.code == 200) {
                            this.$message({
                                type: "success",
                                message: "删除成功"
                            });
                            this.onLoad(this.page)
                        } else {
                            this.$message({
                                type: "error",
                                message: res.data.message
                            });
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
				// .then(() => {
				// 	this.deleteItemFn(row.id)
				// });
            },
            // 获取销方和货方
            getSellBuyerFn() {
                let selectData = []
                getSellBuyer(0,1).then(res => {
                    if (res.data.code == 200) {
                        let dicData = res.data.data
                        dicData.forEach((item, index) => {
							let data = {
                                label: item.name,
                                value: item.id,
                                disabled: false
                            }
                            selectData.push(data) 
                        })
						getSellBuyer(2,2).then(res => {
						    if (res.data.code == 200) {
						        let dicData1 = res.data.data
						        dicData1.forEach((item, index) => {
									let data ={
						                label: item.name,
						                value: item.id,
						                disabled: false
						            }
						            selectData.push(data)
						        })
						        this.dicBuyer = selectData
						        this.dicSell = selectData
						        this.option.column[0].dicData = this.dicSell
						        this.option.column[1].dicData = this.dicBuyer
						    }
						})
                    }
                })
            },
            addItemFn(data, done) {
                let obj = {
                    sellsSupplierId: data.sellsSupplierId,
                    buysSupplierId: data.buysSupplierId,
                    proportion: data.proportion
                }
                addItem(obj).then(res => {
                    if (res.data.code == 200) {
                        if (done) {
                            this.$message({
                                type: "success",
                                message: "添加成功"
                            });
                            this.onLoad(this.page)
                            done()
                        }
                    } else {
                        this.$message({
                            type: "error",
                            message: res.data.message
                        });
                        this.onLoad(this.page)
                        done()
                    }
                })
            },
            editItemFn(data, done) {
                let obj = {
                    sellsSupplierId: data.sellsSupplierId,
                    buysSupplierId: data.buysSupplierId,
                    proportion: data.proportion,
                    id: data.id
                }
                editItem(obj).then(res => {
                    if (res.data.code == 200) {
                        if (done) {
                            this.$message({
                                type: "success",
                                message: "修改成功"
                            });
                            this.onLoad(this.page)
                            done()
                        }
                    } else {
                        this.$message({
                            type: "error",
                            message: res.data.message
                        });
                        done()
                    }
                })
            },
            deleteItemFn(id) {
                let obj = {
                    id: id
                }
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    deleteItem(obj).then(res => {
                        if (res.data.code == 200) {
                            this.$message({
                                type: "success",
                                message: "删除成功"
                            });
                            this.onLoad(this.page)
                        } else {
                            this.$message({
                                type: "error",
                                message: res.data.message
                            });
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            }
        },
    }
</script>
<style lang="scss">

</style>